//
//  BaseViewController.m
//  Main
//
//  Created by wangbaoping on 2022/4/14.
//

#import "BaseVc.h"
#import "BaseApi.h"
#import "UIView+Toast.h"
#import "MBProgressHUD.h"


@interface BaseVc ()<ApiDalegate,MBProgressHUDDelegate>

@end

@implementation BaseVc

MBProgressHUD *progressHud;

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -48, self.view.bounds.size.width, 48)];
    //设置成颜色
    statusBarView.backgroundColor=[UIColor colorWithRed:0/255. green:149/255. blue:135/255. alpha:1];
    // 添加到 navigationBar 上
    [self.navigationController.navigationBar addSubview:statusBarView];
    [self onLoadData];
}

-(void)onLoadData{
    if([self onNeedProgress]){
        [self showProgress];
    }
}

-(bool)onNeedProgress{
    return true;
}

-(void)onApiFailed:(ResponseModel*)model{
    if (!progressHud.hidden) {
        [self hideProgress];
    }
    NSString *hint  = [NSString stringWithFormat:@"%@ | %@",model.code,model.msg];
    [self.view makeToast:hint duration:2.0 position:CSToastPositionCenter];
}

-(void)onApiSuccess:(id)model{
    if (!progressHud.hidden) {
        [self hideProgress];
    }
}

-(void)showProgress{
    progressHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    progressHud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    progressHud.bezelView.color = [UIColor colorWithWhite:0.f alpha:0.f];
    progressHud.delegate = self;
}

-(void) hideProgress {
    [progressHud hideAnimated:YES];
}

#pragma mark -
#pragma mark HUD的代理方法,关闭HUD时执行
-(void)hudWasHidden:(MBProgressHUD *)hud
{
    [progressHud removeFromSuperview];
    progressHud = nil;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
